/*
 * SupplierService.java
 * 
 * Created: Aug 4, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.web.service;

import com.emex.app.shared.data.Repayment;
import com.emex.app.shared.data.RepaymentExample;
import com.emex.app.shared.data.Supplier;
import com.emex.app.shared.data.SupplierMoney;
import com.emex.app.shared.data.SupplierMoneyExample;
import com.emex.app.shared.persistance.RepaymentMapper;
import com.emex.app.shared.persistance.SupplierMapper;
import com.emex.app.shared.persistance.SupplierMoneyMapper;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Supplier money - 0 means payment by client, 1 - payment to supplier 2 -
 * payment by shop 3 - returned payment
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private SupplierMoneyMapper supplierMoneyMapper;

    @Autowired
    private RepaymentMapper repaymentMapper;

    public int deleteSupplierByPrimaryKey(Integer id) {
        return supplierMapper.deleteByPrimaryKey(id);
    }

    public Supplier insertSupplier(Supplier record) {
        supplierMapper.insert(record);
        return record;
    }

    public List<Supplier> getAllSuppliers() {
        return supplierMapper.selectByExample(null);
    }

    public Supplier updateSupplier(Supplier record) {
        supplierMapper.updateByPrimaryKey(record);
        return record;
    }

    public Supplier selectSupplierByPrimaryKey(Integer id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    public int deleteSupplierMoney(Integer id) {
        return supplierMoneyMapper.deleteByPrimaryKey(id);
    }

    public SupplierMoney updateSupplierMoney(SupplierMoney record) {
        record.setDate(correctDate(record.getDate()));
        supplierMoneyMapper.updateByPrimaryKey(record);
        return record;
    }

    public SupplierMoney insertSupplierMoney(SupplierMoney record) {
        record.setDate(correctDate(record.getDate()));
        supplierMoneyMapper.insert(record);
        return record;
    }

    public List<SupplierMoney> getPaymentsFromClientBySupplierId(Integer id) {
        SupplierMoneyExample example = new SupplierMoneyExample();
        example.createCriteria().andSupplierIdEqualTo(id).andTypeEqualTo(0);
        List<SupplierMoney> res = supplierMoneyMapper.selectByExample(example);
        correctDateSM(res);
        return res;
    }

    public List<SupplierMoney> getPaymentsForSupplierBySupplierId(Integer id) {
        SupplierMoneyExample example = new SupplierMoneyExample();
        example.createCriteria().andSupplierIdEqualTo(id).andTypeEqualTo(1);
        List<SupplierMoney> res = supplierMoneyMapper.selectByExample(example);
        correctDateSM(res);
        return res;
    }

    public List<SupplierMoney> getPaymentsByShopBySupplierId(Integer id) {
        SupplierMoneyExample example = new SupplierMoneyExample();
        example.createCriteria().andSupplierIdEqualTo(id).andTypeEqualTo(2);
        List<SupplierMoney> res = supplierMoneyMapper.selectByExample(example);
        correctDateSM(res);
        return res;

    }

    public List<SupplierMoney> getReturnedPaymentsBySupplierId(Integer id) {
        SupplierMoneyExample example = new SupplierMoneyExample();
        example.createCriteria().andSupplierIdEqualTo(id).andTypeEqualTo(3);
        List<SupplierMoney> res = supplierMoneyMapper.selectByExample(example);
        correctDateSM(res);
        return res;
    }

    public int deleteRepayment(Integer id) {
        return repaymentMapper.deleteByPrimaryKey(id);
    }

    public Repayment insertRepayment(Repayment record) {
        record.setDate(correctDate(record.getDate()));
        repaymentMapper.insert(record);
        return record;
    }

    public Repayment updateRepayment(Repayment record) {
        record.setDate(correctDate(record.getDate()));
        repaymentMapper.updateByPrimaryKey(record);
        return record;
    }

    public List<Repayment> getRepaymetsForSupplier(Integer supplierId) {
        RepaymentExample example = new RepaymentExample();
        example.createCriteria().andSupplierIdEqualTo(supplierId);
        List<Repayment> res = repaymentMapper.selectByExample(example);
        correctDate(res);
        return res;
    }

    private void correctDate(List<Repayment> list) {
        if (null != list && !list.isEmpty()) {
            for (Repayment record : list) {
                record.setDate(correctDate(record.getDate()));
            }
        }
    }

    private void correctDateSM(List<SupplierMoney> list) {
        if (null != list && !list.isEmpty()) {
            for (SupplierMoney record : list) {
                record.setDate(correctDate(record.getDate()));
            }
        }
    }

    private Date correctDate(Date op) {
        if (null != op) {
            final long hours12 = 4L * 60L * 60L * 1000L;
            Date date = new Date(op.getTime() + hours12);
            return date;
        }
        return null;
    }
}
